package com.demodis.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.demodis.dao.ConResDaoI;
import com.demodis.dao.DeterPersonDaoI;
import com.demodis.dao.GroupDaoI;
import com.demodis.dao.PersDutyDaoI;
import com.demodis.dao.PositionDaoI;
import com.demodis.dao.persSugDaoI;
import com.demodis.dao.impl.ConResDaoImpl;
import com.demodis.dao.impl.DeterPersonDaoImpl;
import com.demodis.dao.impl.GroupDaoImpl;
import com.demodis.dao.impl.PersDutyDaoImpl;
import com.demodis.dao.impl.PositionDaoImpl;
import com.demodis.dao.impl.persSugDaoImpl;
import com.demodis.domain.TblDeterPerson;
import com.demodis.domain.TblGroup;
import com.demodis.domain.TblPersDuty;
import com.demodis.domain.TblPersSuggest;
import com.demodis.domain.TblUser;
import com.demodis.service.DetContentServiceI;
import com.demodis.service.DeterPersonServiceI;
import com.demodis.service.PermissionServiceI;
import com.demodis.util.SystemConstant;

public class DeterPersonServiceImpl implements DeterPersonServiceI {

	PermissionServiceI permService = new PermissionServiceImpl();
	DeterPersonDaoI deterPerDao = new DeterPersonDaoImpl();
	PersDutyDaoI psersDutyDao = new PersDutyDaoImpl();
	DetContentServiceI detContentService = new DetContentServiceImpl();
	persSugDaoI persSugDao = new persSugDaoImpl();
	ConResDaoI conResDao = new ConResDaoImpl();
	PositionDaoI posDao = new PositionDaoImpl();
	GroupDaoI groupDao = new GroupDaoImpl();

	@Override
	public boolean save(TblDeterPerson t) {
		deterPerDao.save(t);
		return conResDao.save(t.getUserId());
	}

	@Override
	public boolean delete(TblDeterPerson t) {
		// deterPerDao.delete(t);
		// return subjResDao.delete(t.getIds());
		return deterPerDao.delete(t);
	}

	@Override
	public boolean modify(TblDeterPerson t) {
		return deterPerDao.modify(t);
	}

	@Override
	public Object get(TblDeterPerson deterPer, TblUser user) {
		if(deterPer.getPosId() == 0) {
			deterPer.setPosId(posDao.getFirstPosId());
		}
		//List<TblDeterPerson> list = deterPerDao.get(deterPer);
		List<TblDeterPerson> list = this.getFilterAfterDeterPer(deterPer, user);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", deterPerDao.getTotal(deterPer));
		map.put("rows", list);
		return map;
	}

	@Override
	public Object getDeterPerInfo(TblDeterPerson t) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("duty", psersDutyDao.get(t.getUserId()));
		return map;
	}

	@Override
	public synchronized boolean doDeter(TblDeterPerson deterPerson, TblUser user) {
		JSONObject jsonObj = JSON.parseObject(deterPerson.getJsonStr());
		jsonObj.put("doDeterUserId", user.getUserId());
		
		deterPerDao.doDeter(jsonObj);
		
		TblPersSuggest ps = new TblPersSuggest();
		ps.setDetperId(jsonObj.getIntValue("userId"));
		ps.setDeterId(user.getUserId());
		ps.setPsCont(deterPerson.getPsCont());

		return persSugDao.save(ps);
	}
	
	public boolean isExist(int detPerId, int userId) {
		return persSugDao.isExist(detPerId, userId);
	}

	@Override
	public Object queryDeterStat(TblDeterPerson deterPer, int userId, int groupId) {
		List<TblGroup> groupList = null;
		if(deterPer.getGroupId() == null) {
			groupList = this.getGroupList(userId, groupId);
			deterPer.setGroupId(groupList.get(0).getGroupId());
		}
		List<TblDeterPerson> deterPerList = deterPerDao.getByGroupId(deterPer);
		for(TblDeterPerson dp : deterPerList) {//detproOpt
			dp.setScoreMap(detContentService.jishuan(dp));
		}
		return deterPerList;
	}

	@Override
	public Object getPersSug(int userId) {
		return persSugDao.get(userId);
	}

	@Override
	public boolean perExist(int userId) {
		return deterPerDao.perExist(userId);
	}

	@Override
	public List<Map<String, Object>> getGroupEmpApp() {
		return deterPerDao.getGroupEmpApp();
	}

	@Override
	public boolean addAppScore(Map<String, Integer> map) {
		if(map.get("id") == 0) {
			return deterPerDao.addAppScore(map);
		}else {
			return deterPerDao.modifyAppScore(map);
		}
	}

	@Override
	public Object get() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TblGroup> getGroupList(Integer userId, Integer groupId) {
		List<TblGroup> list = null;
		if(permService.existPerm(userId, QDSAURL)) {
			list = groupDao.getGroup(null);
		} else if(permService.existPerm(userId, QDSFURL)) {
			list = groupDao.getFenguan(userId);
		} else if(permService.existPerm(userId, QDSOURL)) {
			list = new ArrayList<TblGroup>();
			TblGroup group = groupDao.getByGroupId(groupId);
			if(group != null) {
				list.add(group);
			}
		}
		return list;
	}

	@Override
	public List<TblDeterPerson> getFilterAfterDeterPer(TblDeterPerson deterPer, TblUser user) {
		
		if("admin".equals(user.getUsername())) {
			return deterPerDao.allDeterUser(deterPer, user.getUserId());
		}
		
		switch (deterPerDao.getUserPosIdByUserId(user.getUserId())) {
		case SystemConstant.PRESIDENTANDSECRETARIATPOSID:
			return deterPerDao.presidentAndSecretariatCanDeterUser(deterPer, user.getUserId());
			
		case SystemConstant.SCHOOLLEADERPOSID:
			
			return deterPerDao.schoolLeaderCanDeterUser(deterPer, user.getUserId());
		case SystemConstant.TWOLEVELLEADERPOSID:
			
			return deterPerDao.twoLeveLeaderCanDeterUser(deterPer, user.getUserId());
		default:
			return deterPerDao.ortherCanDeterUser(deterPer, user.getUserId());
		}
	}

}
